﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IMS_Data;

namespace IMS_WEB
{
    public partial class ViewCustomers : System.Web.UI.Page
    {
        string queryValue = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsCallback)
            {
                queryValue = string.Format("select * from company where company_name like '%{0}%'",TextBox1.Text);
            }
        }
        private void Bind(string name)
        {

            CompanyReposity pr = new CompanyReposity();

            var dataSource = pr.QueryCompany(name);

            GridView1.DataSourceID = null;
            GridView1.DataSource = dataSource;
            GridView1.DataBind();
        }
        protected void Button5_Click(object sender, EventArgs e)
        {
            queryValue = string.Format("select * from company where company_name like '%{0}%'", TextBox1.Text);
            Bind(queryValue);
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bind(queryValue);
        }
        private company CreateCompany(int RowIndex)
        {
            int id = Convert.ToInt32(GridView1.DataKeys[RowIndex].Value);

            company c = new company { index_id = id };

            return c;
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            company c = CreateCompany(e.RowIndex);

            OrderReposity or = new OrderReposity();
            if (or.IsCompanyInOrder(c.index_id))
            {
                e.Cancel = true;
                Response.Write("<script>alert('" + "该厂商已存在于订单中，不能删除" + "')</script>");
                return;
            }


            CompanyReposity cr = new CompanyReposity();
            cr.DeleteCompany(c);
            Bind(queryValue);
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;

            Bind(queryValue);
        }
        private bool CheckDataValid(int RowIndex, ref string errInfo)
        {
            bool result = true;

            string name = ((TextBox)GridView1.Rows[RowIndex].Cells[1].Controls[0]).Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                errInfo += "厂商名称不能为空;";
                result = false;
            }
            string person = ((TextBox)GridView1.Rows[RowIndex].Cells[2].Controls[0]).Text.Trim();

            if (string.IsNullOrEmpty(person))
            {
                errInfo += "企业法人不能为空;";
                result = false;
            }
            string address = ((TextBox)GridView1.Rows[RowIndex].Cells[3].Controls[0]).Text.Trim();
            if (string.IsNullOrEmpty(address))
            {
                errInfo += "厂商地址不能为空;";
                result = false;
            }
            string phone = ((TextBox)GridView1.Rows[RowIndex].Cells[4].Controls[0]).Text.Trim();
            if (string.IsNullOrEmpty(phone))
            {
                errInfo += "厂商联系电话不能为空;";
                result = false;
            }

            return result;
        }
       
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
           
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Response.Redirect(@"~\NewCustomer.aspx");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal
                    || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[GridView1.Columns.Count - 1].Controls[0]).Attributes.Add("onclick",
                        "javascript:return confirm('你确认要删除该厂商吗？')");
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "编辑")
            {
                int row = Convert.ToInt32(e.CommandArgument);
                string name = GridView1.Rows[row].Cells[1].Text.Trim();
                
                Response.Redirect(string.Format("~/UpdateCustomer.aspx?Name={0}",name));
               
            }
        }
    }
}